package com.jvtd.eyl.ui.main.home.classInfo;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BaseMvpActivity;
import com.jvtd.eyl.bean.binding.ClassBean;
import com.jvtd.eyl.databinding.ActivityClassInfoBinding;
import com.jvtd.eyl.ui.main.home.classInfo.classPeriodical.ClassPeriodicalFragment;
import com.jvtd.widget.viewPager.pagerAdapter.JvtdFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.inject.Inject;

/**
 * 期刊分类信息
 *
 * 作者:chenlei
 * 时间:2018/11/13 4:25 PM
 */
public class ClassInfoActivity extends BaseMvpActivity implements ClassInfoMvpView{
    @Inject
    ClassInfoPresenter<ClassInfoMvpView> mPresenter;

    private static final String CLASS_KEY = "CLASS_KEY";
    private ClassBean mClassBean;//一级类

    private ActivityClassInfoBinding mBinding;

    private List<ClassBean> mSecondaryClassList;//二级类列表

    private List<Fragment> mFragments;

    private JvtdFragmentPagerAdapter mAdapter;

    public static Intent getIntent(Context context, ClassBean classBean) {
        Intent intent = new Intent(context, ClassInfoActivity.class);
        intent.putExtra(CLASS_KEY,classBean);
        return intent;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void getLayout() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_class_info);
    }

    @Override
    protected void onDestroy() {
        mPresenter.onDetach();
        super.onDestroy();
    }

    @Override
    protected void initViewAndData() {
        mPresenter.onAttach(this);

        mClassBean = getIntent().getParcelableExtra(CLASS_KEY);
        if (mClassBean == null){
            onError(R.string.error_message);
            return;
        }

        mBinding.setItemBean(mClassBean);

        setToolbar(mBinding.toolBar.toolBar, true);

        Objects.requireNonNull(mBinding.recyclerView.getAdapter()).setOnItemClickListener((adapter, view, position) -> selectViewPager(position));

        mPresenter.getSecondaryClassList(mClassBean.getId());
    }

    /**
     * 选择二级分类期刊
     * @param position 二级分类位置
     */
    private void selectViewPager(int position) {
        for (int i = 0; i < mSecondaryClassList.size(); i++) {
            ClassBean bean = mSecondaryClassList.get(i);
            bean.setSelected(i == position);
        }
        mBinding.viewPager.setCurrentItem(position,false);
    }

    /**
     * 获取二级分类成功
     * @param classBeans 二级分类实体
     */
    @Override
    public void getSecondaryClassListSuccess(List<ClassBean> classBeans) {
        mSecondaryClassList = classBeans;
        initView();
    }

    /**
     * 创建分类期刊列表页
     *
     * @author Chenlei
     * created at 2018/11/13
     **/
    private void initView() {
        mFragments = new ArrayList<>();
        for (ClassBean bean : mSecondaryClassList) {
            mFragments.add(ClassPeriodicalFragment.newInstance(mClassBean.getId(), bean.getId()));
        }
        mBinding.recyclerView.setData(mSecondaryClassList);

        mAdapter = new JvtdFragmentPagerAdapter(getSupportFragmentManager(), mFragments);
        mBinding.viewPager.setAdapter(mAdapter);
        //造成OMM 减小预加载
        mBinding.viewPager.setOffscreenPageLimit(3);

        selectViewPager(0);
    }
}
